function [DivorceCostsM,DivorceCostsF,DivorceCostsMF] = get_divorce_costs(singlefemale,singlemale,couple,leisurem,leisuref,hworkm, hworkf, consumption,wagem,wagef,nonlabor,ub)
% LP to get divorce costs 
 
%defining constants:
workh = 112;        % total potential working hours
num_of_couples = length(find(couple)); %number of couples in the data set
num_of_hh = length(couple); %total number of households in the data.

%% DEFINING DECISION VARIABLES
privateqM = sdpvar(num_of_hh,1);
privateqF = sdpvar(num_of_hh,1);
privatemhworkm = sdpvar(num_of_hh,1);
privatemhworkf = sdpvar(num_of_hh,1);
privatefhworkm = sdpvar(num_of_hh,1);
privatefhworkf = sdpvar(num_of_hh,1);
privateleisurem = sdpvar(num_of_hh,1);
privateleisuref = sdpvar(num_of_hh,1);

aleisurem = sdpvar(1);
aleisuref = sdpvar(1);
ahworkm = sdpvar(1);
ahworkf = sdpvar(1);
aq = sdpvar(1);

nl_incomeM = sdpvar(num_of_hh,1);
nl_incomeF = sdpvar(num_of_hh,1);

divorce_costsM = sdpvar(num_of_couples,1); % divorce costs are defined for every couple given every possible outside option including married and single potential partners
divorce_costsF = sdpvar(num_of_couples,1); % divorce costs are defined for every couple given every possible outside option including married and single potential partners
divorce_costsMF = sdpvar(num_of_hh,num_of_hh,'full'); % divorce costs are defined for every couple given every possible outside option including married and single potential partners


%% SETTING UP THE CONSTRAINTS
Constraints = [];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% private consumption should be balanced %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Hicksian good
for i=1:num_of_hh
    Constraints=[Constraints, ...
        consumption(i)*(1-aq) == privateqM(i) + privateqF(i), privateqM(i) >=0, privateqF(i) >= 0];
end
for i=1:num_of_hh
      if singlefemale(i) == 1
            Constraints = [Constraints, ...
                privateqM(i) == 0];
        else
            if singlemale(i) == 1
               Constraints = [Constraints,...
                  privateqF(i) == 0];
            end
      end 
end

% Domestic work by male 
for i=1:num_of_hh
    Constraints=[Constraints, ...
        hworkm(i)*(1-ahworkm) == privatemhworkm(i) + privatemhworkf(i), privatemhworkm(i) >= 0, privatemhworkf(i) >= 0];
end
for i=1:num_of_hh
      if singlefemale(i) == 1
            Constraints = [Constraints, ...
                privatemhworkm(i) == 0];
        else
            if singlemale(i) == 1
               Constraints = [Constraints,...
                  privatemhworkf(i) == 0];
            end
      end 
end

% Domestic work by female
for i=1:num_of_hh
    Constraints=[Constraints, ...
        hworkf(i)*(1-ahworkf) == privatefhworkm(i) + privatefhworkf(i), privatefhworkm(i) >=0 , privatefhworkf(i) >= 0];
end
for i=1:num_of_hh
      if singlefemale(i) == 1
            Constraints = [Constraints, ...
                privatefhworkm(i) == 0];
        else
            if singlemale(i) == 1
               Constraints = [Constraints,...
                  privatefhworkf(i) == 0];
            end
      end 
end

% Leisure male
for i=1:num_of_hh
    Constraints=[Constraints, ...
        leisurem(i)*(1-aleisurem) == privateleisurem(i)];
end

% Leisure female
for i=1:num_of_hh
    Constraints=[Constraints, ...
        leisuref(i)*(1-aleisuref) == privateleisuref(i)];
end

% balanced nonlabor income:
for i=1:num_of_hh
        if couple(i) == 1
            Constraints = [Constraints, nonlabor(i) == nl_incomeM(i)+nl_incomeF(i)];
        else
            if singlefemale(i) == 1
                Constraints = [Constraints, nonlabor(i) == nl_incomeF(i)];
            else
                if singlemale(i) == 1
                Constraints = [Constraints, nonlabor(i) == nl_incomeM(i)];
                end
            end 
        end
end

% bounding the possible sharing of nonlabor income between partners [.4,.6]
for i=1:num_of_hh
    % check that dealing with couple
    if couple(i) == 1
        if nonlabor(i)>=0
            Constraints = [Constraints,
                .4*nonlabor(i) <= nl_incomeM(i) <= .6*nonlabor(i), .4*nonlabor(i) <= nl_incomeF(i) <= .6*nonlabor(i)];
        else 
            Constraints = [Constraints,
                .6*nonlabor(i) <= nl_incomeM(i) <= .4*nonlabor(i), .6*nonlabor(i) <= nl_incomeF(i) <= .4*nonlabor(i)];
        end
    end
end

% INDIVIDUAL RATIONALITY:
for i=1:num_of_couples
        % IR-M
        Constraints = [Constraints,
            (wagem(i)*workh)*divorce_costsM(i) + nl_incomeM(i) <= privateleisurem(i)*wagem(i) + privatemhworkm(i)*wagem(i) + privatefhworkm(i)*wagef(i) + privateqM(i) + hworkm(i)*ahworkm*wagem(i) + hworkf(i)*ahworkf*wagef(i) + aq*consumption(i) + leisurem(i)*aleisurem*wagem(i) + leisuref(i)*aleisuref*wagef(i)];
        % IR-W
        Constraints = [Constraints,
            (wagef(i)*workh)*divorce_costsF(i) + nl_incomeF(i) <= privateleisuref(i)*wagef(i) + privatemhworkf(i)*wagem(i) + privatefhworkf(i)*wagef(i) + privateqF(i) + hworkm(i)*ahworkm*wagem(i) + hworkf(i)*ahworkf*wagef(i) + aq*consumption(i) + leisurem(i)*aleisurem*wagem(i) + leisuref(i)*aleisuref*wagef(i)];
end

% NO BLOCKING PAIR CONDITIONS
for i=1:num_of_couples
    for j=1:num_of_hh
        if (couple(i)== 1) && (couple(j)==1)
            Constraints = [Constraints, 
                (wagem(i) + wagef(j))*workh*divorce_costsMF(i,j) + nl_incomeM(i) + nl_incomeF(j) <= privateleisurem(i)*wagem(i) + privatemhworkm(i)*wagem(i) + privatefhworkm(i)*wagef(j) + privateqM(i) + privateleisuref(j)*wagef(j) + privatemhworkf(j)*wagem(i) + privatefhworkf(j)*wagef(j) + privateqF(j) + max(hworkm(i),hworkm(j))*ahworkm*wagem(i) + max(hworkf(i),hworkf(j))*ahworkf*wagef(j) + aq*max(consumption(i),consumption(j)) + max(leisurem(i),leisurem(j))*aleisurem*wagem(i) + max(leisuref(i),leisuref(j))*aleisuref*wagef(j)];
        end
        if (couple(i)==1) && (singlefemale(j)==1)
           Constraints = [Constraints, 
           (wagem(i) + wagef(j))*workh*divorce_costsMF(i,j) + nl_incomeM(i) + nl_incomeF(j) <= privateleisurem(i)*wagem(i) + privatemhworkm(i)*wagem(i) + privatefhworkm(i)*wagef(j) + privateqM(i) + privateleisuref(j)*wagef(j) + privatemhworkf(j)*wagem(i) + privatefhworkf(j)*wagef(j) + privateqF(j) + max(hworkm(i),hworkm(j))*ahworkm*wagem(i) + max(hworkf(i),hworkf(j))*ahworkf*wagef(j) + aq*max(consumption(i),consumption(j)) + max(leisurem(i),leisurem(j))*aleisurem*wagem(i) + max(leisuref(i),leisuref(j))*aleisuref*wagef(j)];
        end
       if (couple(i)==1) && (singlemale(j)==1)
                    Constraints = [Constraints, 
           (wagem(j) + wagef(i))*workh*divorce_costsMF(j,i) + nl_incomeM(j) + nl_incomeF(i) <= privateleisurem(j)*wagem(j) + privatemhworkm(j)*wagem(j) + privatefhworkm(j)*wagef(i) + privateqM(j) + privateleisuref(i)*wagef(i) + privatemhworkf(i)*wagem(j) + privatefhworkf(i)*wagef(i) + privateqF(i) + max(hworkm(j),hworkm(i))*ahworkm*wagem(j) + max(hworkf(j),hworkf(i))*ahworkf*wagef(i) + aq*max(consumption(j),consumption(i)) + max(leisurem(j),leisurem(i))*aleisurem*wagem(j) + max(leisuref(j),leisuref(i))*aleisuref*wagef(i)];
        end
    end
end
   

%BOUNDING THE DIVORCE COSTS
% PART FOR IR-M and IR-W
for i=1:num_of_couples
    Constraints = [Constraints, 0 <= divorce_costsM(i) <= 1, 0 <= divorce_costsF(i) <= 1];
end
% FOR NBP: (m_i,w_j)
for i=1:num_of_hh
    for j=1:num_of_hh
         Constraints = [Constraints,  0 <= divorce_costsMF(i,j) <= 1];
    end
end

for i=1:num_of_hh
    for j=1:num_of_hh
        % bounds on divorce costs work only for m_i and w_j who are in the consideration set of each other
         if (couple(i) == 1) && (singlemale(j) == 1)
             Constraints = [Constraints, divorce_costsMF(i,j) == 0];
         end
         if (couple(j) == 1) && (singlefemale(i) == 1)
             Constraints = [Constraints, divorce_costsMF(i,j) == 0];
         end
         if (couple(i) == 0) && (couple(j) == 0)
             Constraints = [Constraints, divorce_costsMF(i,j) == 0];
         end
         Constraints = [Constraints,  0 <= divorce_costsMF(i,j) <= 1];
         
    end
end

Constraints = [Constraints,  0 <= aq <= 1, 0 <= aleisurem <= ub, 0 <= aleisuref <= ub, 0 <= ahworkm <= 1, 0 <= ahworkf <= 1];


%% Specifying Objective Function
Objective = sum(divorce_costsM)+ sum(divorce_costsF)+ sum(divorce_costsMF(:));

% Solving the problem
options = sdpsettings('verbose',0,'solver','gurobi');

% Solve the problem
% -Objective because the default is to minimize while we want to maximize.
sol = optimize(Constraints,-Objective,options);

% Analyze error flags
if sol.problem ~= 0
 display('something went wrong!');
end

%getting the value out
DivorceCostsM  = value(divorce_costsM);
DivorceCostsF  = value(divorce_costsF);
DivorceCostsMF = value(divorce_costsMF);

return
